package com.xu.blog_after_end.service;

import com.xu.blog_after_end.dao.CommentRepository;
import com.xu.blog_after_end.po.Comment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentRepository commentRepository;
    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        List<Comment> comments = commentRepository.findByBlogIdAndParentCommentNull(blogId, sort);
        return eachComment(comments);
    }
    @Transactional
    @Override
    public Comment saveComment(Comment comment) {
        Long parentCommentId = comment.getParentComment().getId();  // 获取传递过来的 parentComment.id
        System.out.println("Received Parent Comment ID: " + parentCommentId);  // 打印接收到的 parentComment.id

        if (parentCommentId != -1) {  // 如果是回复评论，关联父评论
            Comment parentComment = commentRepository.getOne(parentCommentId);
            if (parentComment != null) {
                comment.setParentComment(parentComment);  // 设置父评论
            } else {
                System.out.println("Parent comment not found, setting parentComment to null");
                comment.setParentComment(null);  // 如果没有找到父评论，设为 null
            }
        } else {
            comment.setParentComment(null);  // 根评论没有父评论
        }

        // 设置其他评论信息
        comment.setCreateTime(new Date());
        return commentRepository.save(comment);  // 保存评论
    }



    /**
     * 循环每个顶级的评论节点
     * @param comments
     * @return
     */
     private List<Comment> eachComment(List<Comment> comments) {
        List<Comment> commentsView = new ArrayList<>();
        for (Comment comment : comments) {
            Comment c = new Comment();
            BeanUtils.copyProperties(comment, c);
            commentsView.add(c);
        }
        combineChildren(commentsView);
        return commentsView;
    }

    /**
     *
     * @param comments root根节点，blog不为空的对象集合
     * @return
     */
     private void combineChildren(List<Comment> comments) {
        for (Comment comment : comments) {
            List<Comment> replys1 = comment.getReplyComments();
            for (Comment reply1 : replys1) {
                recursively(reply1);
            }
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();
        }
    }

    //存放迭代找出的所有子代的集合
     private List<Comment> tempReplys = new ArrayList<>();
    /**
     * 递归迭代，剥洋葱
     * @param comment 被迭代的对象
     * @return
     */
     private void recursively(Comment comment) {
        tempReplys.add(comment);
        if (comment.getReplyComments().size() > 0) {
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplyComments().size() > 0) {
                    recursively(reply);
                }
            }
        }
    }
}
